home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / makeicon / tn120_gworld.c < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.7 KB  |  191 lines

  1. /*
  2.     File:        TN120_Gworld.c
  3.  
  4.     Contains:    THE CODE BELOW HERE WAS TAKEN STRAIGHT FROM TECH NOTE #120.
  5.                 SEE THAT TECH NOTE FOR MORE INFORMATION.  
  6.                 (I did remove one parameter from set up pix map.)
  7.  
  8.     Written by:     
  9.  
  10.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include <QDOffscreen.h>
  26. #include <Memory.h>
  27.  
  28. #ifndef topLeft
  29. #define topLeft(r)                      (((Point *) &(r))[0])
  30. #endif
  31.  
  32. #ifndef botRight
  33. #define botRight(r)                     (((Point *) &(r))[1])
  34. #endif
  35.  
  36. GWorldPtr CreateOffScreen (WindowPtr basePort, short depth, Boolean UseTempMem);
  37.  
  38. OSErr SetUpPixMap(
  39.     short        depth,       /* Desired number of bits/pixel in off-screen */
  40.     Rect         *bounds,     /* Bounding rectangle of off-screen */
  41.     CTabHandle   colors,      /* Color table to assign to off-screen */
  42.     PixMapHandle aPixMap      /* Handle to the PixMap being initialized */
  43.     );
  44.     
  45. #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
  46.  
  47. OSErr SetUpPixMap(
  48.     short        depth,       /* Desired number of bits/pixel in off-screen */
  49.     Rect         *bounds,     /* Bounding rectangle of off-screen */
  50.     CTabHandle   colors,      /* Color table to assign to off-screen */
  51.     PixMapHandle aPixMap      /* Handle to the PixMap being initialized */
  52.     )
  53. {
  54.     CTabHandle newColors;       /* Color table used for the off-screen PixMap */
  55.     Ptr        offBaseAddr;     /* Pointer to the off-screen pixel image */
  56.     OSErr      error;           /* Returns error code */
  57.     short      bytesPerRow;        /* Number of bytes per row in the PixMap */
  58.  
  59.  
  60.     error = noErr;
  61.     newColors = nil;
  62.     offBaseAddr = nil;
  63.  
  64.       bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4;
  65.  
  66.    /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
  67.     if (depth <= 8)
  68.         {
  69.         newColors = colors;
  70.         error = HandToHand((Handle *) &newColors);
  71.         }
  72.     else
  73.         {
  74.         newColors = (CTabHandle) NewHandle(sizeof(ColorTable) -
  75.                 sizeof(CSpecArray));
  76.         error = MemError();
  77.         }
  78.     if (error == noErr)
  79.         {
  80.         /* Allocate pixel image; long integer multiplication avoids overflow */
  81.         offBaseAddr = NewPtr((unsigned long) bytesPerRow * (bounds->bottom -
  82.                 bounds->top));
  83.         if (offBaseAddr != nil)
  84.             {
  85.             /* Initialize fields common to indexed and direct PixMaps */
  86.             (**aPixMap).baseAddr = offBaseAddr;  /* Point to image */
  87.             (**aPixMap).rowBytes = bytesPerRow | /* MSB set for PixMap */
  88.                     0x8000;
  89.             (**aPixMap).bounds = *bounds;        /* Use given bounds */
  90.             (**aPixMap).pmVersion = 0;           /* No special stuff */
  91.             (**aPixMap).packType = 0;            /* Default PICT pack */
  92.             (**aPixMap).packSize = 0;            /* Always zero in mem */
  93.             (**aPixMap).hRes = kDefaultRes;      /* 72 DPI default res */
  94.             (**aPixMap).vRes = kDefaultRes;      /* 72 DPI default res */
  95.             (**aPixMap).pixelSize = depth;       /* Set # bits/pixel */
  96.             (**aPixMap).planeBytes = 0;          /* Not used */
  97.             (**aPixMap).pmReserved = 0;          /* Not used */
  98.  
  99.             /* Initialize fields specific to indexed and direct PixMaps */
  100.             if (depth <= 8)
  101.                 {
  102.                 /* PixMap is indexed */
  103.                 (**aPixMap).pixelType = 0;       /* Indicates indexed */
  104.                 (**aPixMap).cmpCount = 1;        /* Have 1 component */
  105.                 (**aPixMap).cmpSize = depth;     /* Component size=depth */
  106.                 (**aPixMap).pmTable = newColors; /* Handle to CLUT */
  107.                 }
  108.             else
  109.                 {
  110.                 /* PixMap is direct */
  111.                 (**aPixMap).pixelType = RGBDirect; /* Indicates direct */
  112.                 (**aPixMap).cmpCount = 3;          /* Have 3 components */
  113.                 if (depth == 16)
  114.                     (**aPixMap).cmpSize = 5;       /* 5 bits/component */
  115.                 else
  116.                     (**aPixMap).cmpSize = 8;       /* 8 bits/component */
  117.                 (**newColors).ctSeed = 3 * (**aPixMap).cmpSize;
  118.                 (**newColors).ctFlags = 0;
  119.                 (**newColors).ctSize = 0;
  120.                 (**aPixMap).pmTable = newColors;
  121.                 }
  122.             }
  123.         else
  124.             error = MemError();
  125.         }
  126.     else
  127.         newColors = nil;
  128.  
  129.     /* If no errors occured, return a handle to the new off-screen PixMap */
  130.     if (error != noErr)
  131.         {
  132.         if (newColors != nil)
  133.             DisposeCTable(newColors);
  134.         }
  135.  
  136.     /* Return the error code */
  137.     return error;
  138.     }
  139.  
  140.  
  141. GWorldPtr CreateOffScreen (WindowPtr basePort, short depth, Boolean UseTempMem)
  142. {
  143.     CGrafPtr    currPort;    /* Pointer to the saved port */
  144.     GDHandle    currGDevice; /* Handle to the current GDevice */
  145.     GWorldPtr   offScreen;   /* Pointer to our GWorld */
  146.     Rect        offRect;     /* portRect of basePort in global coordinates */
  147.     GWorldFlags tempFlags;   /* Flag indicating if temp memory is to be used */
  148.     QDErr       result;      /* Error return from NewGWorld */
  149.  
  150.     GetGWorld (&currPort, &currGDevice);
  151.     SetPort (basePort);
  152.  
  153.     /* Globalize portRect of basePort */
  154.     offRect = basePort->portRect;
  155.     if (depth == 0)
  156.     {
  157.         LocalToGlobal (/*◊*/&topLeft(offRect));
  158.         LocalToGlobal (/*◊*/&botRight(offRect));
  159.     }
  160.  
  161.     /* Set tempFlags to reflect desire for temporary memory */
  162.     if (UseTempMem)
  163.         tempFlags = useTempMem;
  164.     else
  165.         tempFlags = 0;
  166.  
  167.     /* Create the new off-screen port and set it as the current port */
  168.     result = NewGWorld (/*<*/&offScreen, depth, &offRect, nil, nil, tempFlags);
  169.     if (result == noErr)
  170.     {
  171.         SetGWorld (offScreen, (GDHandle) nil);
  172.  
  173.         /* Good idea to set the clip region to the portRect of the new GWorld */
  174.         ClipRect (&offScreen->portRect);
  175.  
  176.         /* Clear the new GWorld to white */
  177.         if (LockPixels (GetGWorldPixMap (offScreen)))
  178.             {
  179.             EraseRect (&offScreen->portRect);
  180.             UnlockPixels (GetGWorldPixMap (offScreen));
  181.             }
  182.         }
  183.     else
  184.         offScreen = nil;
  185.  
  186.     /* Reset current GrafPort and GDevice to what it was when we were called */
  187.     SetGWorld (currPort, currGDevice);
  188.     return offScreen;
  189. }
  190.  
  191.